package com.cskaoyan.service;


import com.cskaoyan.bean.*;
import com.cskaoyan.mapper.*;
import com.cskaoyan.vo.AdminVo;
import com.cskaoyan.vo.PermissionsVo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class AdminServiceImpl implements AdminService {
    @Autowired
    AdminMapper adminMapper;
    @Autowired
    ChildrenbeanMapper childrenbeanMapper;
    @Autowired
    ChildrenbeanxMapper childrenbeanxMapper;
    @Autowired
    SystempermissionsbeanMapper systempermissionsbeanMapper;
    @Autowired
    AssignedpermissionsMapper assignedpermissionsMapper;

    @Override
    public AdminVo list(Integer page, Integer limit, String sort, String order) {

        PageHelper.startPage(page, limit); //mybatis在构建sql语句 👉 会构造limit和offset
//        AdminExample adminExample = new AdminExample();
        List<AdminTemp> adminTemps = adminMapper.selectByAdminTemp();
        PageInfo<AdminTemp> pageInfo = new PageInfo<>(adminTemps);
//        for (int i = 0; i < adminTemps.size() - 1; i++) {
//
//        }
        long total = pageInfo.getTotal();
        AdminVo adminVo = new AdminVo();
        adminVo.setItems(adminTemps);
        adminVo.setTotal(total);
        return adminVo;
    }

    @Override
    public AdminVo list(Integer page, Integer limit, String sort, String order, String username) {
        PageHelper.startPage(page, limit); //mybatis在构建sql语句 👉 会构造limit和offset
//        AdminExample adminExample = new AdminExample();
        List<AdminTemp> adminTemps = adminMapper.selectByAdminTemp();
        PageInfo<AdminTemp> pageInfo = new PageInfo<>(adminTemps);
//        for (int i = 0; i < adminTemps.size() - 1; i++) {
//        }
        long total = pageInfo.getTotal();
        AdminVo adminVo = new AdminVo();
        adminVo.setItems(adminTemps);
        adminVo.setTotal(total);
        return adminVo;
    }

    @Override
    public void update(Admin admin) {

        adminMapper.updateByPrimaryKey(admin);
    }

    @Override
    public void delete(Admin admin) {
        adminMapper.deleteByPrimaryKey(admin.getId());
    }




    @Override
    public Admin create(Admin admin) {
        adminMapper.insert(admin);
        return admin;
    }

    @Autowired
    Childrenbean childrenbean;
    @Override
    public PermissionsVo permissions() {
        ChildrenbeanExample childrenbeanExample = new ChildrenbeanExample();
        List<Childrenbean> childrenbeans = childrenbeanMapper.selectByExample(childrenbeanExample);
        ChildrenbeanxExample childrenxbeanExample1 = new ChildrenbeanxExample();
        List<Childrenbeanx> childrenbeanxes = childrenbeanxMapper.selectByExample(childrenxbeanExample1);
        SystempermissionsbeanExample systempermissionsbeanExample = new SystempermissionsbeanExample();
        List<Systempermissionsbean> systempermissionsbeans = systempermissionsbeanMapper.selectByExample(systempermissionsbeanExample);
        AssignedpermissionsExample assignedpermissionsExample = new AssignedpermissionsExample();
        List<Assignedpermissions> assignedpermissions = assignedpermissionsMapper.selectByExample(assignedpermissionsExample);
        Permissions.SystemPermissionsBean.ChildrenBeanX childrenBeanX = new Permissions.SystemPermissionsBean.ChildrenBeanX();
        childrenBeanX.getChildren(childrenbeans);
        Permissions.SystemPermissionsBean systemPermissionsBean = new Permissions.SystemPermissionsBean();
        systemPermissionsBean.setChildren((List<Permissions.SystemPermissionsBean.ChildrenBeanX>) childrenBeanX);

        Permissions permissions = new Permissions();
        permissions.setSystemPermissions((List<Permissions.SystemPermissionsBean>) systemPermissionsBean);

        PermissionsVo permissionsVo = new PermissionsVo();

        permissionsVo.setPermissions((List<Permissions>) permissions);
        permissionsVo.setTotal(((List<Permissions>) permissions).size());

        return permissionsVo;
    }

    @Override
    public boolean insertLogin(String ip) {
        String adminName = (String) SecurityUtils.getSubject().getPrincipal();
        Log log = new Log(null, adminName, ip, 1, "登录",
                true, "登录成功", "认证通过", new Date(), new Date(), false);
        //logMapper.insertSelective(log);
        return true;
    }


}
